﻿using StarPizza.Model.BaseModel;
using StarPizza.ViewModel.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.Model.User
{
    abstract class UserFactory
    {
        public abstract string Login { get; }
        protected abstract string Password { get; }
        public static bool BaseUserExists(StarDataDataContext db, string login)
        {
            return db.BaseUser.Where(user => user.Login == login).Any();
        }
        public static BaseUser ProvideBaseUser(StarDataDataContext db, string login, string encryptedPassword)
        {
            if (!BaseUserExists(db, login))
            {
                BaseUser baseUser = new BaseUser 
                {
                    Login = login,
                    Password = encryptedPassword
                };
                db.BaseUser.InsertOnSubmit(baseUser);
                Utility.TrySubmitChanges(db);
            }
            return db.BaseUser.Where(user => user.Login == login).First();
        }
        public void EnsureUser(StarDataDataContext db)
        {
            ProvideBaseUser(db, Login, UserAuthenticator.EncryptPassword(Password));
        }
    }
    class ManagerFactory : UserFactory
    {
        public override string Login { get { return "manager"; } }
        protected override string Password { get { return "starmanager"; } }
    }
    class ChefFactory : UserFactory
    {
        public override string Login { get { return "chef"; } }
        protected override string Password { get { return "starchef"; } }
    }
}
